<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class PingjiaController extends Controller
{
	//添加页面
	public function add()
	{
	    $title = '评价添加页面';
	    return view('home.pingjia.add',['title' => $title]);
	}

	//执行添加页面
	public function insert(Request $request)
	{
		$data = $request -> except('_token','x','y','jg','zl','ms');
    	$data_z = $request -> all();

    	//设置限制条件
    	$this -> validate($request, [
	            	 'jg' => 'required',
	             	 'zl' => 'required',
	             	 'ms' => 'required',
    		],[
    		'jg.required' => '请为价格合理评星',
    		'zl.required' => '请为商品质量评星',
    		'ms.required' => '请为描述相符评星',

    		]);

    	//处理评星
		$num = $data_z['jg'] + $data_z['jg'] + $data_z['jg'];
		
		if($num <= '5')
		{
			$data['zonghe'] = '差评';
		}else if($num <= '10' && $num > '5')
		{
			$data['zonghe'] = '中评';
		}else if($num > '10')
		{
			$data['zonghe'] = '好评';
		}

		//处理uid(用户ID) goodname(商品名称) shopname(店铺名称) ptime(评价时间)
		$data['username'] = '二师兄';
		$data['goodname'] = '无敌乞丐裤';
		$data['shopname'] = '二师兄精品屋';
		$data['ptime'] = time();

    	//执行添加
    	$res = DB::table('pingjia') -> insert($data);
    	//判断
		if($res)
		{
			return redirect('/home/pingjia/index') -> with('success', '添加成功');
		}else
		{
			return back() -> with('error', '添加失败');
		}
    
	}

	//列表页
	public function index()
	{
		$title = '评价列表';
		$data = DB::table('pingjia') -> get();

		return view('/home/pingjia/index',['title' => $title,'data' => $data]);
	}
    
}
